package com.neusoft.elmboot.controller;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.elmboot.po.Business;
import com.neusoft.elmboot.po.Comment;
import com.neusoft.elmboot.po.Food;
import com.neusoft.elmboot.po.Orders;
import com.neusoft.elmboot.po.User;
import com.neusoft.elmboot.service.BusinessService;
import com.neusoft.elmboot.util.RSAUtils;

import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;


@RestController
@RequestMapping("/businesses")
public class BusinessController {
	
	RSAUtils rsaUtils=new  RSAUtils();
	
	@Autowired
	private BusinessService businessService;
	
	@GetMapping("/order-types/{orderTypeId}")
	public List<Business> listBusinessByOrderTypeId(Business business) throws Exception{
		return businessService.listBusinessByOrderTypeId(business.getOrderTypeId());
	}
	
	@GetMapping("/{businessId}")
	public Business getBusinessById(Business business) throws Exception{
		return businessService.getBusinessById(business.getBusinessId());
	}
	
	@GetMapping("/search")
	public List<Business> listBusinessByWords(String words)throws Exception{
		return businessService.listBusinessByWords(words);
	}
	
	@PostMapping("/authenticate")
	public Business getBusinessByIdByPass(Business business) throws Exception{
		// 使用私钥解密用户密码
		String privateKeyBase64=businessService.getPrivateKey( business.getBusinessPassId());
        RSA rsa = new RSA(AsymmetricAlgorithm.RSA_ECB_PKCS1.getValue(), privateKeyBase64, null);  // 放入私钥来生成对象
        String password=rsa.decryptStr(business.getPassword(), KeyType.PrivateKey);
        String hashedPassword=DigestUtils.md5DigestAsHex((password +  business.getBusinessPassId()).getBytes());//md5加密，手机号为盐
        hashedPassword=DigestUtils.md5DigestAsHex((hashedPassword +  business.getBusinessPassId()).getBytes());//二次加密
        hashedPassword=DigestUtils.md5DigestAsHex((hashedPassword +  business.getBusinessPassId()).getBytes());//三次加密
        business.setPassword(hashedPassword);  
		// 返回找到的用户对象
		return businessService.getBusinessByIdByPass(business);
	}
	
	
	@PostMapping("/{businessPassId}")
	public String getBusinessByPassId(Business business) throws Exception{
	    int flag=businessService.getBusinessByPassId(business.getBusinessPassId());
	    //判断用户是否存在
	    if(flag==0) {
	    	//返回公钥
	    	String[]keysBase64=rsaUtils.rsa_jiami(business);
	    	business.setPublicKey(keysBase64[0]);
	    	business.setPrivateKey(keysBase64[1]);
	    	businessService.saveKey(business);
	    	return keysBase64[0]; 
	    }
	    else return "1";
	}
	
	@PostMapping("")
	public int saveBusiness(Business business) throws Exception{
		String privateKeyBase64=businessService.getPrivateKey(business.getBusinessPassId());
        RSA rsa = new RSA(AsymmetricAlgorithm.RSA_ECB_PKCS1.getValue(), privateKeyBase64, null);  // 放入私钥来生成对象
        String password=rsa.decryptStr(business.getPassword(), KeyType.PrivateKey);//明文密码
        String hashedPassword=DigestUtils.md5DigestAsHex((password + business.getBusinessPassId()).getBytes());//md5加密，手机号为盐
        hashedPassword=DigestUtils.md5DigestAsHex((hashedPassword + business.getBusinessPassId()).getBytes());//二次加密
        hashedPassword=DigestUtils.md5DigestAsHex((hashedPassword + business.getBusinessPassId()).getBytes());//三次加密
        business.setPassword(hashedPassword); 
		return businessService.saveBusiness(business);
	}
	

	@GetMapping("/key/{businessPassId}")
	public String getPublicteKey(Business business) throws Exception{
		return businessService.getPublicKey(business.getBusinessPassId());
	}
	
	@PutMapping("/{businessPassId}")
	public int updateBusiness(Business business) throws Exception{
		//String password=business.getPassword();
		if(business.getPassword()==null||business.getPassword()=="") {}
		else{
			String password=rsaUtils.rsa_endjiemi(businessService.getPrivateKey(business.getBusinessPassId()),business.getPassword());
			business.setPassword(rsaUtils.md5jiami(password,business.getBusinessPassId())); 
		}
		return businessService.updateBusiness(business);
	} 
	
	
	@GetMapping("/all/{businessPassId}")
	public Business getEntireBusinessById(Business business) {
		return businessService.getEntireBusinessById(business.getBusinessPassId());
	}
	
	
	
}
